import requests
import lxml
import lxml.etree


class Spider():
    def __init__(self, book_name):
        self.book_name = book_name
        self.search_url = 'https://www.52bqg.com/modules/article/search.php' + \
            '?searchkey=%C5%CC%C1%FA'
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
        }

    def get_content(self):
        # params = {
        #     "searchkey": self.book_name,
        # }
        response = requests.get(
            url=self.search_url,
            headers=self.headers,
        )
        # 解决乱码
        response.encoding = response.apparent_encoding
        mytree = lxml.etree.HTML(response.text)
        return mytree

    def search_book(self):
        html = self.get_content()

        book_url=[]
        for book in html.xpath('//div[@id="main"]//li//span[@class="s2"]//a'):
            if self.book_name == book.xpath('text()')[0]:
                book_url.append(book.xpath('@href')[0])

        print(book_url)




if __name__ == "__main__":
    Spider = Spider('盘龙')
    Spider.search_book()
